<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, target-densitydpi=device-dpi">
    <link rel='stylesheet' href='/admin/stylesheets/base.css' />
    <link rel='stylesheet' href='/admin/stylesheets/addItem.css' />
</head>
<body>
<div class="wrap">
    <form action="" method="post" id="itemForm">
        <p>
            <span>选项标题:</span>
            <br>
            <input id="title" name="title" type="text">
        </p>
        <p>
            <span>选项图片:</span>
            <br>
            <button class="add_img" type="button">添加图片</button>
        </p>
        <p>
            <span>初始票数:</span>
            <br>
            <input id="vote_num" name="vote_num" type="number">
        </p>
        <p><button class="submit" type="button">提交</button></p>
    </form>

    <form action="/admin/upload" method="post" enctype="multipart/form-data" id="upload_form">
        <input type="file" name="file" id="file" />
    </form>
    <script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $('.submit').on('click', function(){
            $.ajax({
                url: "/admin/addItem",
                type: "post",
                dataType: "json",
                data: $('#itemForm').serialize(),
                success: function(data){
                    alert(data.msg);
                    if(data.code == 1002){
                        window.location.reload();
                    }
                },
                error: function(){}
            })
        });

        $('.add_img').on('click', function(){
            $('#file').click();
        });

        $('#file').on('change', function(){
//            $('#upload_form').submit();
            $.ajax({
                url: '/admin/upload',
                type: 'post',
                data: $('#upload_form').serialize(),
                dataType: 'json',
                success: function(data){
                    alert(data.msg);
                },
                error: function(){}
            })
        });
    </script>
</div>
</body>
</html>
